package unicom.hadoop.demo.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


@SuppressWarnings("unchecked")
public class BaseAction extends ActionSupport {


	private static final long serialVersionUID = 1L;
	private String path;

	public String getPath() {
		return ServletActionContext.getRequest().getContextPath();
	}
	
	public HttpServletRequest getHttpRequest() {
		HttpServletRequest request = ServletActionContext.getRequest();
		return request;
	}


	public HttpSession getHttpSession() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		return session;
	}


	public HttpServletResponse getHttpResponse() {
		HttpServletResponse response = ServletActionContext.getResponse();
		return response;
	}


	public ActionContext getActionContext() {
		return ActionContext.getContext();
	}


	public Map getSessionMap() {
		return getActionContext().getSession();
	}

	public Map getApplicationMap() {
		return getActionContext().getApplication();
	}

	public Map getActionContextMap() {
		return getActionContext().getContextMap();
	}


	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}


	public Object getBeanByName(String beanName) {
		ApplicationContext ac = WebApplicationContextUtils
				.getRequiredWebApplicationContext(getServletContext());
		return ac.getBean(beanName);
	}

	
	public void print(Object text) {
		try {
			PrintWriter writer = this.getHttpResponse().getWriter();
			writer.print(String.valueOf(text));
		} catch (IOException e) {
			e.printStackTrace();
		}

	}


	public String transcode(String str) {
		String formatStr = "";
		try {
			formatStr = new String(str.getBytes("ISO-8859-1"), "utf-8");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return formatStr;
	}


	public void renderXML(String xml) {
		try {
			HttpServletResponse response = getHttpResponse();
			response.setContentType("text/xml;charset=UTF-8");
			response.getWriter().write(xml);
			response.flushBuffer();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


	public List getFirstElementOfList(List<Object[]> sourceList) {

		List result = new ArrayList();
		if (sourceList == null || sourceList.size() == 0)
			return result;
		for (Object[] objArr : sourceList) {
			result.add(objArr[0]);
		}
		return result;

	}
	
	public void sendRedirect(String url) {
		try {
			this.getHttpResponse().sendRedirect(path+url);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
